package com.reremouse.rerebbs.dao.impl;

import com.reremouse.rerebbs.dao.IImgDAO;
import com.reremouse.rerebbs.dao.ISystemConfigDAO;
import com.reremouse.service.ImageService;
import com.reremouse.util.Cat;
import com.reremouse.util.SpringHelper;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import java.io.*;

public class ImgDAO
        implements IImgDAO, Serializable {
    ISystemConfigDAO systemConfigDAO;

    public ISystemConfigDAO getSystemConfigDAO() {
        return this.systemConfigDAO;
    }

    public void setSystemConfigDAO(ISystemConfigDAO systemConfigDAO) {
        this.systemConfigDAO = systemConfigDAO;
    }

    public String saveImg(File localFile) {
        try {
            File f0 = localFile;
            byte[] bb = new byte[512];
            int i = 0;
            InputStream is = new FileInputStream(f0);
            String ofn = f0.getName();
            int t = ofn.lastIndexOf('.');
            String ext = ofn.substring(t, ofn.length());
            String ext1 = ".pip2";
            String nfn = "/resources/sys/" + Cat.getUniqueId() + ext1;
            String afp = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath(nfn);
            File f1 = new File(afp);
            FileOutputStream fos = new FileOutputStream(f1);
            while ((i = is.read(bb)) != -1) {
                fos.write(bb, 0, i);
            }
            is.close();
            fos.close();
            return nfn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void saveImg(InputStream fis, long id) {
        try {
            byte[] bb = new byte[1024];
            String dir = "";
            boolean f = this.systemConfigDAO.getSystemConfig().getIfRelative().booleanValue();
            if (f) {
                String tp = this.systemConfigDAO.getSystemConfig().getFilePath();
                if (!tp.endsWith("/")) {
                    tp = tp + "/";
                }
                dir = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath(tp);
            } else {
                dir = this.systemConfigDAO.getSystemConfig().getFilePath();
            }
            if (!dir.endsWith("\\")) {
                dir = dir + "\\";
            }
            File f_dir0 = new File(dir);
            if (!f_dir0.exists()) {
                f_dir0.mkdir();
            }
            dir = dir + "user_images";

            File f_dir = new File(dir);
            if (!f_dir.exists()) {
                f_dir.mkdir();
            }
            System.out.println("user image store path:" + dir);


            String nfn = dir + "\\" + id;
            File ffff = new File(nfn);
            FileOutputStream fos = new FileOutputStream(ffff);


            BufferedInputStream is = new BufferedInputStream(fis);
            BufferedOutputStream os = new BufferedOutputStream(fos);
            int len = 0;
            while ((len = is.read(bb)) != -1) {
                os.write(bb, 0, len);
            }
            is.close();
            os.close();
            fis.close();

            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void delImg(Long id) {
        String dir = "";
        boolean f = this.systemConfigDAO.getSystemConfig().getIfRelative().booleanValue();
        if (f) {
            String tp = this.systemConfigDAO.getSystemConfig().getFilePath();
            if (!tp.endsWith("/")) {
                tp = tp + "/";
            }
            dir = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath(tp);
        } else {
            dir = this.systemConfigDAO.getSystemConfig().getFilePath();
        }
        if (!dir.endsWith("\\")) {
            dir = dir + "\\";
        }
        String rp = dir;
        String nfn = rp + "\\" + id;
        File f1 = new File(nfn);
        if (f1.exists()) {
            f1.delete();
        }
        ImageService iss = (ImageService) SpringHelper.getSpringBean("ImageService");
        iss.deleteRelativeImage((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(), id);
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.dao.impl.ImgDAO

 * JD-Core Version:    0.7.0.1

 */